package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate
{

    //在支付宝创建的应用的id
    private String app_id = "2021000148654074";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDxbc0ePIO41XOQsfjyCNjZMLQmTq7LcF3pQ48pXESC6LgPjxcrpuq/1vsXTt+VlKKcF5uaasQB7Cl2qiPUxfqYbOUWBK571CEegVuS5pz+QZGNeDDKHCMtFcUdTEXi5Khr7RXIpfkAcHzSj6+EOlM4ARkCjoncM8Es0oUJyaMppM+9faOlUX4F+LKuj/DzNuoFcHY7h9yB9MLN3vnO8V1EYESojnf5OQ1xn3uv/f3BBLdx+52mtMWuxo4ilvYc3i3gjqGWFesg59RCmD0ql38sR3snfJpv7MPqqPcZF4b58fGoWtEQN9eccb2TbjUsxSZ7Xo1EJQlTWR2KCQgHFxjxAgMBAAECggEAKaKL6xj4hlmZMmzex66q5tm1V53eGwtnxqB2AeX9kq3bgCNnFYoVlbvXVW+uszcMy+puVTTwVHyLsgEpaLozuzGcWC26gXRVudMJHc5TaK2aElGV6GTt1AqbEbBjpPtV6DbcuNuWk3LSs+U/mYgNbe0SiRRXxffO6pHri4KQds+1m5m7OAqoEbXzKIHCbQ9r9hwghaAIvpCsfvL+K6Ds8rBsWXuPveaYhnNcBmJ4GjMdTYHUlR+IxinT5G0AqqTsJO5juHemjY8oX+SZStBtTPiBNuDmGkD49Ce4M+Wlz8F1oy4oN11ktmR/esQBnusafPoRrn02V0mLOFMAJeLtOQKBgQD6wKmzPHTLkHpWm4UTOgC3Ke/7mniQYcrEQZfkG/G//2RUQVX5NQlTbRuLUbvnGNEq2c6K09pya7y9yH313ThvTCX/O5T/yVr66e6sFcWXHtab7LHyUQh4GXxkzwVRi0qoDyLV3VbFnuFOiDHLM1qg1LPzuPFYcHjrsTXkk59FMwKBgQD2ezB40Xdx/H4F2tj3wN8D2EiP+Mvs40qtmjJm3aXby2iYKFgK8SnT6BGKlL6z1r50naHEyuCIre/PNrJeKsXM2mNGGY4f0bxICjkZ3EouDqsgNM8qJ+HpwjWUEJYt4V+5erCiJXqM/U9tvy+hT+Fz2HxU8b6oZ0uX9cR+zfThSwKBgQCF+7Axb1pQ8xALeunuTg3X0PbED9PgAgDgQNQyNKRGHOa1Bnwzvp0PELupA7FNWDtblCoCkBSnEt404g4780qWXa/n4yRIUITmkVzQYonDp9ywUBb9DUNjOOr4yo1i9vSbPAs3Dx+wVKa62ZcRpo0EZuMIlYeP4/kYpwXO7vopdQKBgQCmXZHjCqdPpbg+JXArEeC1jKe1nZ2vP98o/Bom+lY327y2bwy2Hf84i7xDSJDLfBfDSJ5m4PTa7GQf/fA4xTALTCCTsS4P5hXqnhPak6jyG25/54UpFBBVIoPdc7CaOTPOUrSkTR3ByjxxfAkfHXa0JZwODbm9mzMX14gQet1drwKBgBgW4l0oZiKgduT4aY66uDr8TsVG0tjz5Hhs0Q6pytGslbvyxFUryER2lF6jJvxCKVTfn31BidPkvi/iwQZz8U8jk4zTOG7y3t2XZqq9YDYyHxxE6VlcI7LDE7pMS1MeXis8ZhnjdNedC5jN8dNyFsdWQLwJbftusKG47CTzz4m/";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhzil3zlRUZ6EdBzXTe+AeTj/X6Bh5m40Ag15Rf3FtrC9GxwL815Cp3Jw+DykULmrdIxMEm0T0Kp3pUUqKaPEWB8cMDCdDGCx3Pzcx5pY02JuiaCHdtmbFeUw9zUft9YmDfweTKbpmfy/p/6EjmLHMJCffSbb5NgYCCGkDyQQ0BlEqIHP3e97CTf7TjvQV8fLA/qYRmMCjH3vWModvrYL3lpv6uJYOuqCMr3KFIrQPtA9lR8TLz0iDFNAXkeF1fYnn6LW+6Iibw8TqgDrdGwaHbTkDhlRrwRegCTHXba8hlV+h0JQJ2Z5As61DY2esCU5rMoQarbdpjeZBNZFuyV9SQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://mbf8fc99.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://order.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException
    {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
